package com.guohe3.just.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guohe3.just.DO.Score;
import com.guohe3.just.DO.Student;
import com.guohe3.just.common.enums.CrawTypeEnum;
import com.guohe3.just.craw.CrawService;
import com.guohe3.just.mapper.ScoreMapper;
import com.guohe3.just.mapper.StudentMapper;
import com.guohe3.just.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author 浦希成
 * 2018/10/24
 */
@Service
@Slf4j
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private CrawService crawService;
    @Autowired
    private StudentMapper mapper;


    @Override
    public List<Score> getScoreAll(String username, String password) throws IOException {
        List<Score> scores;
        try {
            scores = crawService.crawScoreAll(username, password, CrawTypeEnum.NORMAL);
        } catch (Exception e) {
            scores = null;
        }
        if (scores == null) {
            scores = crawService.crawScoreAll(username, password, CrawTypeEnum.VPN);
        }
        return scores;
    }


    @Override
    public Student getStudentInfo(String username, String password) throws Exception {
        Student student;
        try {
            student = crawService.crawStudentInfo(username, password, CrawTypeEnum.NORMAL);
        } catch (Exception e) {
            student = null;
        }
        if (student == null) {
            student = crawService.crawStudentInfo(username, password, CrawTypeEnum.VPN);
        }
        return student;
    }

    @Override
    public List<Map<String, String>> getSchoolTimetable(String username, String password, String semester) throws IOException {
        List<Map<String, String>> schoolTimetable;
        try {
            schoolTimetable = crawService.crawSchoolTimetable(username, password, semester, CrawTypeEnum.NORMAL);
        } catch (Exception e) {
            schoolTimetable = null;
        }
        if (schoolTimetable == null) {
            schoolTimetable = crawService.crawSchoolTimetable(username, password, semester, CrawTypeEnum.VPN);
        }
        return schoolTimetable;
    }

    @Override
    public List<Map<String, String>> getJidian(String username, String password) throws IOException {
        List<Map<String, String>> jidian;
        try {
            jidian = crawService.crawJidian(username, password, CrawTypeEnum.NORMAL);
        } catch (Exception e) {
            jidian = null;
        }
        if (jidian == null) {
            jidian = crawService.crawJidian(username, password, CrawTypeEnum.VPN);
        }
        return jidian;
    }

    @Override
    public Student login(String username, String password) throws Exception {
        Student student = mapper.selectByUsernameAndPassword(username, password);
        if (student != null) {
            return student;
        } else {
            return getStudentInfo(username, password);
        }
    }


}
